﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SpeechBill
{
    public partial class Speech : Form
    {
        private SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        private string C_time;//消费日期
        private string C_curtime;//消费具体时间
        private float C_money;//消费金额
        private string C_type;//消费类型
        private string C_mon;
        
        public Speech()
        {
            InitializeComponent();
        }

        public string str_text;
        private void Speech_Load(object sender, EventArgs e)
        { 
            //textBox1.Text = str_text;         
            Handstr(str_text);
            dateTimePicker1.Text = C_time;
            textBox1.Text = C_curtime;
            textBox3.Text = str_text;
            CurrentPosition cur = new CurrentPosition();
            cur.GetLocationEvent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Handstr(string str)
        {
            C_curtime = DateTime.Now.ToString("t");
        
            switch(str){
                case "今天":
                    C_time = DateTime.Now.ToShortDateString().ToString();
                    break;
                case "昨天":
                    C_time = DateTime.Now.AddDays(-1).ToString();
                    break;
                default:
                    C_time = DateTime.Now.ToShortDateString().ToString();
                    break;
            }
            if (str.Contains("今天") == true || str.Contains("昨天") == true)
            {
                str = str.Substring(2);
                if (str.Contains("早上") == true || str.Contains("中午") == true || str.Contains("晚上") == true ||str.Contains("宵夜"))
                {
                    str = str.Substring(4);
                }
                else
                {
                    str = str.Substring(2);
                }
            }
            else
            {
                if (str.Contains("早上") == true || str.Contains("中午") == true || str.Contains("晚上") == true ||str.Contains("宵夜"))
                {
                    str = str.Substring(4);
                }
                else
                {
                    str = str.Substring(2);
                }

            }
            if (str.Contains("元"))
            {
                str = str.Substring(0, str.Length - 1);
            }
            for (int i = 0; i < str.Length; i++)
            {
                if (str.Contains("千") || str.Contains("百") || str.Contains("十"))
                {
                    string str_tem = str.Substring(0, 1);
                    C_mon += str_tem.ToString();
                    str = str.Substring(2);
                    //MessageBox.Show(str_tem);
                }
            }
            C_mon += str.ToString();
            //MessageBox.Show("total:" + C_mon);
            textBox2.Text = C_mon;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            C_time = dateTimePicker1.Text.ToString();
            C_curtime = textBox1.Text.ToString();
            C_money = float.Parse(textBox2.Text.ToString());
            C_type = comboBox1.SelectedItem.ToString();
            
            string cmdText = "insert into consume(c_money,c_type,c_day,c_time) values(N'" + C_money + "',N'" + C_type + "',N'" + C_time + "',N'" + C_curtime + "')";
            
            int count = DBHelper.ExctInsertDelUp(cmdText);
            if (count > 0)
            {
                MessageBox.Show("添加成功");
                //this.button2_Click(sender,e);
                typenumchange(C_type);
                inpos();
            }
            else
            {
                MessageBox.Show("失败");
            }
        }
        public static void typenumchange(string c_type)
        {
            string cmdText = "update contype set typenum = (typenum + 1) from contype where typename = '" + c_type + "'";
            
            int count = DBHelper.ExctInsertDelUp(cmdText);
            if (count > 0)
            {
                MessageBox.Show("success");
            }
        }
        public static void inpos()
        {
            
            string cmdText = "insert into position (db_Latitude,db_Longitude,cu_time) values ('" + CurrentPosition.db_Latitude.ToString() + "','" + CurrentPosition.db_Longitude.ToString() + "','" + DateTime.Now.ToString() + "')";
            int count = DBHelper.ExctInsertDelUp(cmdText);
            if (count > 0)
            {
                MessageBox.Show("添加位置");
            }
            else
            {
                MessageBox.Show("位置添加失败");
            }
        }

    }
}
